(function($) {
	$.fn.pagination = function(options) {
        var opts = $.extend({}, $.fn.pagination.defaults, options);
        return this.each(function() {
            $(this).empty().append(renderpager(parseInt(options.pageNo)
								  ,parseInt(options.pageCount)
								  ,parseInt(options.showPageCount)
								  ,options.buttonClickCallback));
        });
    };
	
    function renderpager(pageNo, pageCount, showPageCount , buttonClickCallback) {
   
        var $pagination = $('<ul class="pagination"></ul>');

        //第一页 上一页
        $pagination.append(renderButton('&laquo;&laquo;', pageNo, pageCount, buttonClickCallback)).append(renderButton('&laquo;', pageNo, pageCount, buttonClickCallback));

        
        var startPoint = 1;
        var endPoint = showPageCount;

        if (pageNo > parseInt(showPageCount/2)) {
            startPoint = pageNo - parseInt(showPageCount/2) + 1;
            endPoint = pageNo + parseInt(showPageCount/2);
        }

        if (endPoint > pageCount) {
            startPoint = pageCount - parseInt(showPageCount/2) -1 ;
            endPoint = pageCount;
        }

        if (startPoint < 1) {
            startPoint = 1;
        }

       
        for (var page = startPoint; page <= endPoint; page++) {

            var currentButton = $('<li><a href="javascript:void(0); data-toggle="tooltip" title="第'+(page)+'页" >' + (page) + '</a></li>');

            page == pageNo ? currentButton.addClass('active') : currentButton.click(function() { buttonClickCallback(this.firstChild.firstChild.data); });
            currentButton.appendTo($pagination);
        }

        // 下一页 最后一页
        $pagination.append(renderButton('&raquo;', pageNo, pageCount, buttonClickCallback)).append(renderButton('&raquo;&raquo;', pageNo, pageCount, buttonClickCallback));
		

		$pagination.tooltip();

        return $pagination;
    }

    function renderButton(buttonLabel, pageNo, pageCount, buttonClickCallback) {

        var destPage = 1;
		var button;

        
        switch (buttonLabel) {
            case "&laquo;&laquo;":	//第一页
                destPage = 1;
				button = $('<li><a href="javascript:void(0); data-toggle="tooltip" title="第一页">' + buttonLabel + '</a></li>');
                break;
            case "&laquo;":			//上一页
                destPage = pageNo - 1;
				button = $('<li><a href="javascript:void(0); data-toggle="tooltip" title="上一页">' + buttonLabel + '</a></li>');
                break;
            case "&raquo;":			//下一页
				button = $('<li><a href="javascript:void(0); data-toggle="tooltip" title="下一页">' + buttonLabel + '</a></li>');
                destPage = pageNo + 1;
                break;
            case "&raquo;&raquo;":		//最后一页
				button = $('<li><a href="javascript:void(0); data-toggle="tooltip" title="最后一页">' + buttonLabel + '</a></li>');
                destPage = pageCount;
                break;
        }
		

        if (buttonLabel == "&laquo;&laquo;" || buttonLabel == "&laquo;") {
            pageNo <= 1 ? button.addClass('disabled') : button.click(function() { buttonClickCallback(destPage); });
        }
        else {
            pageNo >= pageCount ? button.addClass('disabled') : button.click(function() { buttonClickCallback(destPage); });
        }
		
		
        return button;
    }

    $.fn.pagination.defaults = {
        pageNo: 1,		//当前页
        pageCount: 1 ,	//总页数
		showPageCount : 5 , //显示的分页按钮个数
		buttonClickCallback : null	//按钮点击回调
    };
    
    $.fn.pagination.pageNo = function() {
    	return 1;
    };
    
})(jQuery);